﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class PageVersionConfirm : Form
    {
        public PageVersionConfirm(string typeVision)
        {
            InitializeComponent();
            m_typeVersion = typeVision;
        }
        private string m_typeVersion = "";
        private void btn_changeTypeVersion_Click(object sender, EventArgs e)
        {
            var curType = m_typeVersion.Trim().Split(GlobalVar.char_VersionSpilt)[0];
            var curVersion = m_typeVersion.Trim().Split(GlobalVar.char_VersionSpilt)[1];
            if (!string.IsNullOrEmpty(curType) && !string.IsNullOrEmpty(curVersion))
            {
                if (MessageBox.Show("请确认目前设备上的工装是否已更换   工艺参数已确认并正式生产?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (GlobalVar.userConfig.UploadInfo.IsUploadUrl && !string.IsNullOrEmpty(GlobalVar.userConfig.UploadInfo.url_GetModelInfo))
                    {
                        var res = UploadInterface.upload_GetModelInfo();
                        if (res != null && res.Code == "00" && res.ModelInfoList != null && res.ModelInfoList.Count > 0)
                        {
                            var mlst = res.ModelInfoList.Where(a => a.Model == curType).ToList();
                            if (mlst != null && mlst.Count > 0)
                            {
                                var curVer = curVersion;
                                var curConfig = "";
                                if (curVersion.IndexOf(" ") > 0)
                                {
                                    curVer = curVersion.Split(' ')[0];
                                    curConfig = curVersion.Split(' ')[1];
                                }
                                var vlst = mlst[0].ModelVerInfoList.Where(a => a.Ver == curVer && a.Config == curConfig).ToList();
                                if (vlst != null && vlst.Count > 0)
                                {
                                    GlobalVar.ChangeModel(m_typeVersion);
                                    displalyInfo.displayInfoMain($"版本切换为{ GlobalVar.closeSave.CurrentTypeName }");
                                    this.Close();
                                }
                            }
                        }
                    }
                    else
                    {
                        GlobalVar.ChangeModel(m_typeVersion);
                        displalyInfo.displayInfoMain($"版本切换为{ GlobalVar.closeSave.CurrentTypeName }");
                        this.Close();
                    }
                }

            }
        }

        private void PageVersionConfirm_Load(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty( m_typeVersion)&& m_typeVersion.Contains(GlobalVar.char_VersionSpilt))
            {
                var arr_str = m_typeVersion.Split(GlobalVar.char_VersionSpilt);
                if(arr_str.Length>1)
                {
                    var curType = arr_str[0];
                    var curVersion= arr_str[1];
                    if (!string.IsNullOrEmpty(curType) && !string.IsNullOrEmpty(curVersion))
                    {
                            try
                            {
                                var typeVersion = curType + GlobalVar.char_VersionSpilt + curVersion;
                                lbl_typeVersion.Text = typeVersion;
                                var motionConfig = (MotionConfig)SerializeHelper.DeserializeFile<MotionConfig>(GlobalVar.type_path + "\\" + typeVersion + ".tfp", "型号参数");//运动参数反序列化
                                var temp = motionConfig.Cut_pos[(int)e_PosCut.夹膜位];
                                lbl_夹膜位.Text = $"X:{temp.X.ToString("F02")} Y:{temp.Y.ToString("F03")}Z:{temp.Z.ToString("F02")} R1:{temp.R1.ToString("F02")}R2:{temp.R2.ToString("F02")}";
                                temp = motionConfig.Cut_pos[(int)e_PosCut.撕膜过度位];
                                lbl_过渡位.Text = $"X:{temp.X.ToString("F02")} Y:{temp.Y.ToString("F03")}Z:{temp.Z.ToString("F02")} R1:{temp.R1.ToString("F02")}R2:{temp.R2.ToString("F02")}";
                                temp = motionConfig.Cut_pos[(int)e_PosCut.撕膜终点位];
                                lbl_终点位.Text = $"X:{temp.X.ToString("F02")} Y:{temp.Y.ToString("F03")}Z:{temp.Z.ToString("F02")} R1:{temp.R1.ToString("F02")}R2:{temp.R2.ToString("F02")}";

                                var tempLablel = motionConfig.Plate1_pos[(int)e_Pos.飞达取标位];
                                lbl_取标位.Text = $"平台1X:{tempLablel.X.ToString("F02")} Y:{tempLablel.Y.ToString("F02")}Z:{tempLablel.Z.ToString("F02")} Z高:{tempLablel.ZHeight.ToString("F02")}R:{tempLablel.R.ToString("F02")}";
                                tempLablel = motionConfig.Plate2_pos[(int)e_Pos.飞达取标位];
                                lbl_取标位.Text += $"平台2X:{tempLablel.X.ToString("F02")} Y:{tempLablel.Y.ToString("F02")}Z:{tempLablel.Z.ToString("F02")} Z高:{tempLablel.ZHeight.ToString("F02")}R:{tempLablel.R.ToString("F02")}";

                                tempLablel = motionConfig.Plate1_pos[(int)e_Pos.拍照位];
                                lbl_拍照位.Text = $"平台1X:{tempLablel.X.ToString("F02")} Y:{tempLablel.Y.ToString("F02")}Z:{tempLablel.Z.ToString("F02")} Z高:{tempLablel.ZHeight.ToString("F02")}R:{tempLablel.R.ToString("F02")}";
                                tempLablel = motionConfig.Plate2_pos[(int)e_Pos.拍照位];
                                lbl_拍照位.Text += $"平台2X:{tempLablel.X.ToString("F02")} Y:{tempLablel.Y.ToString("F02")}Z:{tempLablel.Z.ToString("F02")} Z高:{tempLablel.ZHeight.ToString("F02")}R:{tempLablel.R.ToString("F02")}";

                                tempLablel = motionConfig.Plate1_pos[(int)e_Pos.OK放标位];
                                lbl_OK放标位.Text = $"平台1X:{tempLablel.X.ToString("F02")} Y:{tempLablel.Y.ToString("F02")}Z:{tempLablel.Z.ToString("F02")} Z高:{tempLablel.ZHeight.ToString("F02")}R:{tempLablel.R.ToString("F02")}";
                                tempLablel = motionConfig.Plate2_pos[(int)e_Pos.OK放标位];
                                lbl_OK放标位.Text += $"平台2X:{tempLablel.X.ToString("F02")} Y:{tempLablel.Y.ToString("F02")}Z:{tempLablel.Z.ToString("F02")} Z高:{tempLablel.ZHeight.ToString("F02")}R:{tempLablel.R.ToString("F02")}";

                                var tempPut = motionConfig.visionPutOK1;
                                lbl_放标补偿位.Text = $"平台1X:{tempPut.X.ToString("F03")} Y:{tempPut.Y.ToString("F03")}R:{tempPut.R.ToString("F03")}";
                                tempPut = motionConfig.visionPutOK2;
                                lbl_放标补偿位.Text += $"平台2X:{tempPut.X.ToString("F03")} Y:{tempPut.Y.ToString("F03")}R:{tempPut.R.ToString("F03")}";

                                tempPut = motionConfig.visionCenter1;
                                lbl_膜中心相对坐标.Text = $"平台1X:{tempPut.X.ToString("F03")} Y:{tempPut.Y.ToString("F03")}R:{tempPut.R.ToString("F03")}";
                                tempPut = motionConfig.visionCenter2;
                                lbl_膜中心相对坐标.Text += $"平台2X:{tempPut.X.ToString("F03")} Y:{tempPut.Y.ToString("F03")}R:{tempPut.R.ToString("F03")}";

                                var template = motionConfig.TF_list[(int)e_CameraPlate.定位相机平台1];
                                lbl_X补偿位.Text = $"平台1X:{template.xMin.ToString("F03")}-{template.xMax.ToString("F03")}";
                                lbl_Y补偿位.Text = $"平台1Y:{template.yMin.ToString("F03")}-{template.yMax.ToString("F03")}";
                                lbl_R补偿位.Text = $"平台1R:{template.rMin.ToString("F03")}-{template.rMax.ToString("F03")}";
                                template = motionConfig.TF_list[(int)e_CameraPlate.定位相机平台2];
                                lbl_X补偿位.Text += $"平台2X:{template.xMin.ToString("F03")}-{template.xMax.ToString("F03")}";
                                lbl_Y补偿位.Text += $"平台2Y:{template.yMin.ToString("F03")}-{template.yMax.ToString("F03")}";
                                lbl_R补偿位.Text += $"平台2R:{template.rMin.ToString("F03")}-{template.rMax.ToString("F03")}";
                                lbl_启用双直线交点定位.Text = motionConfig.IsEnableLinePos ? "启用" : "不启用";
                            }
                            catch(Exception ex)
                            {
                                displalyInfo.displayErrorMain(ex.StackTrace);
                            }
                    }
                }
               
            }
         
        }
    }
}
